<template>
  <view class="goods-list">
    <view @click="goDetail(goods.goods_id)" v-for="(goods,index) in goodsList" :key="index">
      <my-goods :goods="goods"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        // 商品列表数据
        goodsList:[],
        total:0,
        isLoading:false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
      if(this.isLoading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.isLoading  = false
      this.goodsList = []
      // 下拉刷新重新获取数据
      this.getGoodsList(()=> uni.stopPullDownRefresh())
    },
    methods:{
      async getGoodsList(callback){
       
        this.isLoading = true
       const {data: res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
       // 请求完毕关闭节流阀，关闭上拉刷新的动画
       callback && callback() 
        this.isLoading = false
       if(res.meta.status !== 200 ) return uni.$showMsg()
       this.goodsList =this.goodsList.concat(res.message.goods) 
       this.total = res.message.total
      
      },
      goDetail(goodsId){
       uni.navigateTo({
         url:`/subpkg/goods_detail/goods_detail?goods_id=${goodsId}`
       })
      }
    }
  }
</script>

<style lang="scss">

</style>
